/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package media_server;
import java.io.*;
import java.net.*;
import java.text.Format;
import javax.media.MediaLocator;
/**
 *
 * @author DELL
 */
public class TCPServer
{
    
   // public static String S_ip;
    public TCPServer(String S_ip,String S_port1,String S_port2,String S_port3,String S_port4)
    {
       try
        {
                ServerSocket s= new ServerSocket(3200);
                //String S_ip="";
                do
                {
                        Socket ss=s.accept(); //synchronous

                      //  System.out.println(ss.getPort());
                       System.out.println(ss.getRemoteSocketAddress());
                       // S_ip=ss.getRemoteSocketAddress().toString();
                        InputStream is=ss.getInputStream();
                        BufferedReader br=new BufferedReader(new InputStreamReader(is));

                        OutputStream os=ss.getOutputStream();
                        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));


                        String receivedMessage;

                        do
                        {
                                receivedMessage=br.readLine();
                                if (receivedMessage.equalsIgnoreCase("quit"))
                                {
                                        break;
                                }
                                if(receivedMessage.equalsIgnoreCase("request"))
                                {
                                        bw.write(S_ip+"/"+S_port1+"/"+S_port2+"/"+S_port3+"/"+S_port4);
                                        bw.newLine();
                                        bw.flush();
                                      //  System.out.println(S_ip+"/"+S_port1+"/"+S_port2+"/"+S_port3+"/"+S_port4);

                                }
                        }
                        while (true);
                        bw.close();
                        br.close();
                }
                while (true);

        }
        catch(IOException e)
        {
                System.out.println("There're some error");
        }
    }

 public static void main(String [] args) {
	// We need three parameters to do the transmission
	// For example,
	//   java AVTransmit2 file:/C:/media/test.mov  129.130.131.132 42050

	//if (args.length < 3) {
	   // prUsage();
	//}

	Format fmt = null;
	int i = 0;

	// Create a audio transmit object with the specified params.
	AVTransmit2 at = new AVTransmit2(new MediaLocator("file:/G:/Nhac/NhacTre/NhacHinh/anh se co quen em_tuan khanh_o.avi"),
					     "192.168.10.255", "6000", null);
	// Start the transmission
	String result = at.start();

	// result will be non-null if there was an error. The return
	// value is a String describing the possible error. Print it.
	if (result != null)
       {
	    System.err.println("Error : " + result);
	    System.exit(0);
	}

	System.err.println("Start transmission for 60 seconds...");

	// Transmit for 60 seconds and then close the processor
	// This is a safeguard when using a capture data source
	// so that the capture device will be properly released
	// before quitting.
	// The right thing to do would be to have a GUI with a
	// "Stop" button that would call stop on AVTransmit2
	try {
	    Thread.currentThread().sleep(10000000);
	} catch (InterruptedException ie) {
	}

	// Stop the transmission
	at.stop();

	System.err.println("...transmission ended.");

	System.exit(0);
    }

}
